/**
* @author Ashish Gupta
* CVSSP, University of Surrey
* Copyright (c) 2010 Ashish Gupta
* All rights reserved.
*/
/**
 * 
 */
package genetic;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author Ashish
 * The GeneMap class associates hashedCells with Genes. The keys of GeneMap are cell identifiers
 * and the values of GeneMap are gene identifiers.
 */
public class GeneMap implements Map<Integer, Integer>{
	private ArrayList<Integer> keys;
	private ArrayList<Integer> values;
	
	public GeneMap(){
		keys = new ArrayList<Integer>();
		values = new ArrayList<Integer>();
	}

	/* (non-Javadoc)
	 * @see java.util.Map#clear()
	 */
	@Override
	public void clear() {
		keys.clear();
		values.clear();
	}

	/* (non-Javadoc)
	 * @see java.util.Map#containsKey(java.lang.Object)
	 */
	@Override
	public boolean containsKey(Object key) {
		return keys.contains(key);
	}

	/* (non-Javadoc)
	 * @see java.util.Map#containsValue(java.lang.Object)
	 */
	@Override
	public boolean containsValue(Object value) {
		return values.contains(value);
	}

	/* (non-Javadoc)
	 * @see java.util.Map#entrySet()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Set entrySet() {
		return null;
	}

	/* (non-Javadoc)
	 * @see java.util.Map#get(java.lang.Object)
	 */
	@Override
	public Integer get(Object key) {
		int keyidx = keys.indexOf(key);
		if(keyidx == -1){
			return null;
		}
		return values.get(keyidx);
	}

	/* (non-Javadoc)
	 * @see java.util.Map#isEmpty()
	 */
	@Override
	public boolean isEmpty() {
		return size() == 0;
	}

	/* (non-Javadoc)
	 * @see java.util.Map#keySet()
	 */
	@Override
	public Set<Integer> keySet() {
		return new TreeSet<Integer>(keys);
	}

	/* (non-Javadoc)
	 * @see java.util.Map#put(java.lang.Object, java.lang.Object)
	 */
	@SuppressWarnings("unchecked")
	public Integer put(Integer key, Integer value) {
		for(int i=0; i < keys.size(); i++){
			Integer old = keys.get(i);
		
		// does the cellId already exist in the arraylist
			if( ((Comparable<Integer>)keys).compareTo(keys.get(i))==0){
				keys.set(i, (Integer) value);
				return old;
			}
			if(((Comparable<Integer>)key).compareTo(keys.get(i))==1){
				int where = i > 0 ? i-1 : 0;
				keys.add(where,(Integer) key);
				values.add(where,(Integer) value);
				return null;
			}
		}
		keys.add((Integer) key);
		values.add((Integer) value);
		return null;
	}

	/* (non-Javadoc)
	 * @see java.util.Map#putAll(java.util.Map)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void putAll(Map map) {
		Iterator<?> keysItr = map.keySet().iterator();
		while(keysItr.hasNext()){
			Integer key = (Integer) keysItr.next();
			Integer value =  (Integer) map.get(key);
			put(key,value);
		}
	}

	/* (non-Javadoc)
	 * @see java.util.Map#remove(java.lang.Object)
	 */
	@Override
	public Integer remove(Object key) {
		int keyidx = keys.indexOf(key);
		if(keyidx == -1){
			return null;
		}
		Integer oldValue = values.get(keyidx);
		keys.remove(keyidx);
		values.remove(keyidx);
		return oldValue;
	}

	/* (non-Javadoc)
	 * @see java.util.Map#size()
	 */
	@Override
	public int size() {
		return keys.size();
	}

	/* (non-Javadoc)
	 * @see java.util.Map#values()
	 */
	@Override
	public Collection<Integer> values() {
		return values;
	}
	
	/**
	 * @return keys
	 */
	public Collection<Integer> keys(){
		return keys;
	}
	
	/**
	 * @param index
	 * @return key at index
	 */
	public Integer getKey(int index){
		return keys.get(index);
	}

}
